package com.abe.config;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.abe.app.ManagerAPP;
import com.abe.mvp.model.bean.exercise.ExerciseSTGroupBean;
import com.abe.utils.JsonHelper;
import com.abe.utils.SPUtils;
import com.google.gson.annotations.Expose;
import com.google.gson.reflect.TypeToken;

/** 登录用户锻炼报告 */
public class ExerciseSTReportUtil {

	private static String ExerciseSTReport = "ExerciseSTReport";
	private static String ExerciseSTReportEmpty = JsonHelper
			.toJson(new ArrayList<ExerciseSTGroupBean>());

	private static List<ExerciseSTGroupBean> getBeans() {
		String exerciseData = (String) SPUtils.get(ManagerAPP.getContext(),
				ExerciseSTReport, ExerciseSTReportEmpty);
		Type type = new TypeToken<List<ExerciseSTGroupBean>>() {
		}.getType();
		List<ExerciseSTGroupBean> groupBeans = JsonHelper.parseObject(
				exerciseData, type);
		return groupBeans;
	}

	/** 保存登录用户此次锻炼数据 */
	public static void saveExercise(ExerciseSTGroupBean bean) {
		List<ExerciseSTGroupBean> groupBeans = getBeans();
		groupBeans.add(bean);
		SPUtils.put(ManagerAPP.getContext(), ExerciseSTReport,
				JsonHelper.toJson(groupBeans));
	}

	/** 清除此次登录用户锻炼数据 */
	public static void clearExercise() {
		SPUtils.put(ManagerAPP.getContext(), ExerciseSTReport,
				ExerciseSTReportEmpty);
	}

	/** 获取本次登录累计锻炼数据 */
	public static ExerciseSTReportBean getExerciseReport() {
		int count = 0;
		int allWeight = 0;
		float calorie = 0;
		ExerciseSTReportBean reportBean = ExerciseSTReportBean.getReport();
		List<ExerciseSTGroupBean> groupBeans = getBeans();
		int num = groupBeans.size();
		for (int i = 0; i < num; i++) {
			ExerciseSTGroupBean bean = groupBeans.get(i);
			count = count + bean.getCount();
			allWeight = allWeight + bean.getWeight();
			calorie = calorie + bean.getCalorie();
		}
		reportBean.setCount(count);
		reportBean.setCalorie(calorie);
		if (count==0) {
			reportBean.setWeight(0);
		}else {
			reportBean.setWeight(allWeight / count);
		}
		return reportBean;
	}

	/** 获取当前登录用户锻炼数据 */

	public static class ExerciseSTReportBean {
		@Expose
		private int count;
		@Expose
		private int weight;
		@Expose
		private float calorie;

		public int getCount() {
			return count;
		}

		public void setCount(int count) {
			this.count = count;
		}

		public int getWeight() {
			return weight;
		}

		public void setWeight(int weight) {
			this.weight = weight;
		}

		public float getCalorie() {
			return calorie;
		}

		public void setCalorie(float calorie) {
			this.calorie = calorie;
		}

		public static ExerciseSTReportBean getReport() {
			return new ExerciseSTReportBean();
		}
	}
}
